package com.daiyang.lease.web.app.controller.login;


import com.daiyang.lease.common.result.Result;
import com.daiyang.lease.common.utils.JwtUtil;
import com.daiyang.lease.web.app.service.LoginService;
import com.daiyang.lease.web.app.vo.user.LoginVo;
import com.daiyang.lease.web.app.vo.user.UserInfoVo;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "登录管理")
@RequestMapping("/app/")
public class LoginController {
    @Autowired
    LoginService service;

    @GetMapping("login/getCode")
    @Operation(summary = "获取短信验证码")
    public Result getCode(@RequestParam String phone) throws Exception {
        service.sendCode(phone);
        return Result.ok();
    }

    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = service.login(loginVo);
        return Result.ok(token);
    }

    @GetMapping("info")
    @Operation(summary = "获取登录用户信息")
    public Result<UserInfoVo> info(@RequestHeader(value="access_token",required = true) String accessToken) {
        Claims claims = JwtUtil.parseToken(accessToken);

        Integer id = (Integer)claims.get("userId");
        UserInfoVo uesrInfoVo = service.getUserInfoById(id);

        return Result.ok(uesrInfoVo);
    }
}
